import 'dart:async';

import 'package:changxindianzishangwu/else/lib/common/index.dart';
import 'package:changxindianzishangwu/model/time_model.dart';
import 'package:changxindianzishangwu/util/app_color.dart';
import 'package:changxindianzishangwu/util/colors.dart';
import 'package:changxindianzishangwu/util/toast.dart';
import 'package:changxindianzishangwu/util/universal_view.dart';
import 'package:changxindianzishangwu/widget/app_bar.dart';
import 'package:changxindianzishangwu/widget/calendar_dialog.dart';
import 'package:date_format/date_format.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:r_calendar/r_calendar.dart';

class DataCenterCalendarPage extends StatefulWidget {
  const DataCenterCalendarPage({Key key}) : super(key: key);

  @override
  _DataCenterCalendarPageState createState() => _DataCenterCalendarPageState();
}

class _DataCenterCalendarPageState extends State<DataCenterCalendarPage> {
  RCalendarController startController;
  RCalendarController endController;

  CalendarDialog _startDialog;
  CalendarDialog _endDialog;

  String _startTime="";
  String _endTime="";

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    startController = RCalendarController.single(
      selectedDate:  DateTime.now(),
      isAutoSelect: true,
    );
    startController.addListener(() {
      startController.isAutoSelect;
      // print('startController 选择的时间：${startController.selectedDate}');
      setState(() {
        _startTime=formatDate(startController.selectedDate, [yyyy,'-',mm,'-',dd]);
        print('---_startTime-----年月日：$_startTime');
      });
      _startDialog.dismiss();

    });
    endController = RCalendarController.single(
      selectedDate:  DateTime.now(),
      isAutoSelect: true,
    );
    endController.addListener(() {
      endController.isAutoSelect;
      setState(() {
        _endTime=formatDate(endController.selectedDate, [yyyy,'-',mm,'-',dd]);
        print('---_endTime-----年月日：$_endTime');
      });
      _endDialog.dismiss();

    });
    _startDialog=CalendarDialog(startController);
    _endDialog=CalendarDialog(endController);

  }
  @override
  void dispose() {
    // TODO: implement dispose
    endController.dispose();
    startController.dispose();

    super.dispose();
  }


  BuildContext _context;
  @override
  Widget build(BuildContext context) {
    _context=context;
    return Scaffold(
      appBar: TopAppBar(context, "选择时间"),
      body: Column(
        children: [
          Padding(
            padding: const EdgeInsets.all(15.0),
            child: Row(
              children: [
                _timeLayout("选择开始时间",_startTime,_startDialog),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Text(
                    "-",
                    style:
                        TextStyle(fontSize: 20, color: AppColor.colorGrey153),
                  ),
                ),
                _timeLayout("选择截止时间",_endTime,_endDialog),
              ],
            ),
          ),
          Container(
              margin: EdgeInsets.only(top: height(25)),
              child: UniversalView().longBtn("确认", () {
                if(ObjectUtil.isEmpty(_startTime)){
                  toast("请选择开始日期");
                  return;
                }
                if(ObjectUtil.isEmpty(_endTime)){
                  toast("请选择结束日期");
                  return;
                }

                Get.back(result: TimeModel(startTime: _startTime,endTime: _endTime));
              })),
        ],
      ),
    );
  }

  Widget _timeLayout(title,time,dialog) {
    return Expanded(
      flex: 1,
      child: InkWell(
        onTap: () {
          showCustomDialog(_context,dialog);
        },
        child: Container(
          padding: EdgeInsets.all(width(10)),
          decoration: ShapeDecoration(
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(Radius.circular(5)),
                  side: BorderSide(
                      color: AppColor.colorGrey214,
                      style: BorderStyle.solid,
                      width: 1))),
          child: Row(
            children: [
              Expanded(
                flex: 1,
                child: Text(ObjectUtil.isEmptyString(time)?title:time,
                    style: TextStyle(
                        fontSize: sp(15), color: AppColor.colorGrey153)),
              ),
              Image.asset(
                "images/ic_rl_grey.png",
                width: width(15),
                height: width(15),
              )
            ],
          ),
        ),
      ),
    );
  }

  Future showCustomDialog(BuildContext context,dialog) async {
    var result = await showDialog(
        context: context,
        builder: (BuildContext context) {
          return dialog;
        });
    return result;
  }
}
